package Test10;

import java.io.*;
import java.util.Scanner;

public class Test103 {
    public static void main(String[] args) {
        System.out.println("请输入扫描路径：");
        Scanner in=new Scanner(System.in);
        String s=in.next();
        File file=new File(s);
        if(file.isDirectory()){
            System.out.println("请输入指定字符：");
            String st=in.next();
            findFile(file,st);
        }else{
            System.out.println("输入有误");
        }
    }

    private static void findFile(File file, String st) {
        File[] f=file.listFiles();
        if(f==null){
            return ;
        }
        for(int i=0;i<f.length;i++){
            File in=f[i];
            System.out.println(in.getAbsolutePath());
            if(in.isFile()){
                if(in.getName().contains("st")){
                    Scanner s=new Scanner(System.in);
                    System.out.println("是否要删除：Y/N");
                    String choice=s.next();
                    if(choice.toLowerCase().equals("y")){
                        in.delete();
                        System.out.println("删除成功");
                    }else{
                        System.out.println("取消删除");
                    }
                    System.out.println(in.getAbsolutePath());
                }else{
                    if(find(in,st)){
                        System.out.println(in.getAbsolutePath());
                    }
                }
            }else{
                findFile(in,st);
            }
        }
    }

    private static boolean find(File file, String st) {
        try(InputStream inputStream=new FileInputStream(file.getAbsoluteFile())){
            while(true) {
                Scanner in=new Scanner(inputStream);
                String str=in.next();
                if(str.contains(st)){
                    Scanner s=new Scanner(System.in);
                    System.out.println("是否要删除：Y/N");
                    String choice=s.next();
                    if(choice.toLowerCase().equals("y")){
                        file.delete();
                        System.out.println("删除成功");
                        return true;
                    }else{
                        System.out.println("取消删除");
                        return false;
                    }
                }else {
                    return false;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
